package com.example.palette.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.palette.bean.ColorBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class RectangleView extends View {
    public static final int AREA_SELECT_STATUS_SELECTING = 1;
    public static final int AREA_SELECT_STATUS_SELECTED = 2;
    public static final int AREA_SELECT_STATUS_UNSELECTED = 3;
    public static final int PART_SELECT_DIRECTION_LEFT = 1;
    public static final int PART_SELECT_DIRECTION_TOP = 2;
    public static final int PART_SELECT_DIRECTION_RIGHT = 3;
    public static final int PART_SELECT_DIRECTION_BOTTOM = 4;
    public static final int PART_SELECT_STATUS_SELECTING = 1;
    public static final int PART_SELECT_STATUS_SELECTED = 2;
    public static final int PART_SELECT_STATUS_UNSELECTED = 3;
    private int mCurrentAreaSelectStatus = AREA_SELECT_STATUS_UNSELECTED;
    private int mCurrentPartSelectStatus = PART_SELECT_STATUS_UNSELECTED;
    private int mSelectedRetangleIndex = -1;
    private int mPartSelectRetangleIndex = -1;
    private int mPartSelectLine = 3;
    private int mPartSelectRow = 3;
    private int mMaxResistance;
    private int mMinResistance;
    private int mLineSize = 60;
    private int mRowSize = 60;
    private float mSmallRectangleWidth;
    private float mSmallRectangleHeight;
    private float mCurrentScale = 1;
    private float mScaleTranslateX;
    private float mScaleTranslateY;
    private float mTranslateX;
    private float mTranslateY;
    private boolean mIsHistory;
    private boolean mDrawRealTimeAreaOrPartInfo;
    private Paint mSmallRectanglePaint = new Paint();
    private Paint mAreaSelectPaint = new Paint();
    private Paint mPartSelectPaint = new Paint();
    private Paint mPressurePaint = new Paint();
    private List<RectF> mSmallRectangleList = new ArrayList<>();
    private List<Integer> mResistanceList = new ArrayList<>();
    private List<Integer> mAreaSelectPositionList = new ArrayList<>();
    private List<ColorBean> mColorBeanList;
    private RectF mPartSelectOneList = new RectF();
    private RectF mPartSelectTwoList = new RectF();
    private RectF mPartSelectThreeList = new RectF();
    private RectF mPartSelectFourList = new RectF();
    private RectF mPartSelectFiveList = new RectF();
    private Rect mCurrentScaleRect = new Rect();
    private Path mAreaSelectPath = new Path();
    private ScaleGestureDetector mScaleGestureDetector;
    private GestureDetector mGestureDetector;
    private InnerListener mListener;

    public RectangleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mScaleGestureDetector = new ScaleGestureDetector(context, new SGDListener());
        mGestureDetector = new GestureDetector(context, new GDListener());
        mPartSelectPaint.setStyle(Paint.Style.STROKE);
        mPartSelectPaint.setAntiAlias(true);
        mPressurePaint.setStyle(Paint.Style.FILL);
        mAreaSelectPaint.setAntiAlias(true);
        mAreaSelectPaint.setStyle(Paint.Style.STROKE);
        mSmallRectanglePaint.setAntiAlias(true);
        mSmallRectanglePaint.setStyle(Paint.Style.STROKE);
        for (int i = 0; i < mLineSize * mRowSize; i++) {
            mResistanceList.add(0);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        mSmallRectangleWidth = width / (float) mRowSize;
        mSmallRectangleHeight = height / (float) mLineSize;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        mSmallRectangleList.clear();
        float offsetX = 0;
        float offsetY = 0;
        float left;
        float top;
        float right;
        float bottom = 0;
        canvas.getClipBounds(mCurrentScaleRect);
        canvas.translate(mScaleTranslateX, mScaleTranslateY);
        canvas.translate(mTranslateX, mTranslateY);
        mSmallRectanglePaint.setColor(Color.GRAY);
        double totalPressure = 0, areaTotalPressure = 0, p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
        for (int i = 0; i < mLineSize; i++) {
            for (int j = 0; j < mRowSize; j++) {
                int index = i * mLineSize + j;
                int resistance = mResistanceList.get(index);
                left = offsetX;
                top = offsetY;
                right = left + mSmallRectangleWidth * mCurrentScale;
                bottom = top + mSmallRectangleHeight * mCurrentScale;
                RectF rectF = new RectF(left, top, right, bottom);
                mSmallRectangleList.add(rectF);
                canvas.drawRect(rectF, mSmallRectanglePaint);
                if (isValidResistance(resistance)) {
                    double pressure = matchPressureByResistance(resistance);
                    totalPressure += pressure;
                    mPressurePaint.setColor(matchColorByPressure(pressure));
                    canvas.drawRect(rectF, mPressurePaint);
                }
                offsetX = right;
            }
            offsetX = 0;
            offsetY = bottom;
        }
        if (mCurrentAreaSelectStatus == AREA_SELECT_STATUS_SELECTED) {
            mAreaSelectPaint.setColor(Color.BLUE);
            mAreaSelectPaint.setStrokeWidth(mCurrentScale * 3);
            for (int i = 0; i < mLineSize * mRowSize; i++) {
                int resistance = mResistanceList.get(i);
                RectF rectF = mSmallRectangleList.get(i);
                if (isValidResistance(resistance) && isCurrentRectangleInAreaSelect(mAreaSelectPositionList, new PointF(rectF.centerX(), rectF.centerY()))) {
                    areaTotalPressure += matchPressureByResistance(resistance);
                    canvas.drawRect(rectF, mAreaSelectPaint);
                }
            }
            if (!mDrawRealTimeAreaOrPartInfo) {
                mAreaSelectPositionList.clear();
            }
        } else if (mCurrentAreaSelectStatus == AREA_SELECT_STATUS_SELECTING) {
            if (mAreaSelectPositionList.size() > 1) {
                mAreaSelectPaint.setStrokeWidth(mCurrentScale);
                mAreaSelectPaint.setColor(Color.RED);
                mAreaSelectPath.reset();
                for (int k = 0; k < mAreaSelectPositionList.size(); k++) {
                    RectF rectF = mSmallRectangleList.get(mAreaSelectPositionList.get(k));
                    if (mAreaSelectPath.isEmpty()) {
                        mAreaSelectPath.moveTo(rectF.centerX(), rectF.centerY());
                    } else {
                        mAreaSelectPath.lineTo(rectF.centerX(), rectF.centerY());
                    }
                    canvas.drawPath(mAreaSelectPath, mAreaSelectPaint);
                }
            }
        }
        if (mListener != null) {
            mListener.onTotalPressure(mIsHistory, formatDouble(totalPressure));
            mListener.onAreaSelect(formatDouble(areaTotalPressure));
        }
        if (mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTING) {
            int offsetx = 0;
            int offsety = 0;
            int start = mPartSelectRetangleIndex = mPartSelectRetangleIndex == -1 ? 0 : mPartSelectRetangleIndex;
            if (start % mRowSize + mPartSelectRow > mRowSize) {
                offsetx = start % mRowSize + mPartSelectRow - mRowSize;
            }
            if ((start / mRowSize) % mLineSize + mPartSelectLine > mLineSize) {
                offsety = (start / mRowSize) % mLineSize + mPartSelectLine - mLineSize;
            }
            int end = start + mRowSize * (mPartSelectLine - 1 - offsety) + (mPartSelectRow - 1 - offsetx);
            RectF startRectF = mSmallRectangleList.get(start);
            RectF endRectF = mSmallRectangleList.get(end);
            mPartSelectPaint.setStrokeWidth(mCurrentScale * 3);
            mPartSelectPaint.setColor(Color.RED);
            canvas.drawRect(startRectF.left, startRectF.top, endRectF.right, endRectF.bottom, mPartSelectPaint);
        } else if (mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTED) {
            if (mPartSelectRetangleIndex != -1) {
                int offsetx = 0;
                int offsety = 0;
                int start = mPartSelectRetangleIndex;
                if (start % mRowSize + mPartSelectRow > mRowSize) {
                    offsetx = start % mRowSize + mPartSelectRow - mRowSize;
                }
                if ((start / mRowSize) % mLineSize + mPartSelectLine > mLineSize) {
                    offsety = (start / mRowSize) % mLineSize + mPartSelectLine - mLineSize;
                }
                int end = start + mRowSize * (mPartSelectLine - 1 - offsety) + (mPartSelectRow - 1 - offsetx);
                RectF startRectF = mSmallRectangleList.get(start);
                RectF endRectF = mSmallRectangleList.get(end);
                RectF partRectF = new RectF(startRectF.left, startRectF.top, endRectF.right, endRectF.bottom);
                boolean onePartMatch = false, twoPartMatch = false, threePartMatch = false, fourPartMatch = false, fivePartMatch = false;
                boolean oneMatch = false, twoMatch = false, threeMatch = false, fourMatch = false, fiveMatch = false;
                int oneSize = 0, twoSize = 0, threeSize = 0, fourSize = 0, fiveSize = 0;
                if (mPartSelectOneList.isEmpty()) {
                    mPartSelectOneList = partRectF;
                    onePartMatch = true;
                } else if (mPartSelectTwoList.isEmpty()) {
                    mPartSelectTwoList = partRectF;
                    twoPartMatch = true;
                } else if (mPartSelectThreeList.isEmpty()) {
                    mPartSelectThreeList = partRectF;
                    threePartMatch = true;
                } else if (mPartSelectFourList.isEmpty()) {
                    mPartSelectFourList = partRectF;
                    fourPartMatch = true;
                } else if (mPartSelectFiveList.isEmpty()) {
                    mPartSelectFiveList = partRectF;
                    fivePartMatch = true;
                }
                mPartSelectPaint.setColor(Color.BLUE);
                for (int i = 0; i < mLineSize * mRowSize; i++) {
                    RectF rectF = mSmallRectangleList.get(i);
                    int resistance = mResistanceList.get(i);
                    boolean valid = isValidResistance(resistance);
                    if (fivePartMatch) {
                        if (mPartSelectFiveList.contains(rectF) && valid) {
                            fiveMatch = true;
                            fiveSize++;
                            p5 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectFourList.contains(rectF) && valid) {
                            fourMatch = true;
                            fourSize++;
                            p4 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectThreeList.contains(rectF) && valid) {
                            threeMatch = true;
                            threeSize++;
                            p3 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectTwoList.contains(rectF) && valid) {
                            twoMatch = true;
                            twoSize++;
                            p2 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectOneList.contains(rectF) && valid) {
                            oneMatch = true;
                            oneSize++;
                            p1 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                    } else if (fourPartMatch) {
                        if (mPartSelectFourList.contains(rectF) && valid) {
                            fourMatch = true;
                            fourSize++;
                            p4 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectThreeList.contains(rectF) && valid) {
                            threeMatch = true;
                            threeSize++;
                            p3 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectTwoList.contains(rectF) && valid) {
                            twoMatch = true;
                            twoSize++;
                            p2 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectOneList.contains(rectF) && valid) {
                            oneMatch = true;
                            oneSize++;
                            p1 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                    } else if (threePartMatch) {
                        if (mPartSelectThreeList.contains(rectF) && valid) {
                            threeMatch = true;
                            threeSize++;
                            p3 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectTwoList.contains(rectF) && valid) {
                            twoMatch = true;
                            twoSize++;
                            p2 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectOneList.contains(rectF) && valid) {
                            oneMatch = true;
                            oneSize++;
                            p1 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                    } else if (twoPartMatch) {
                        if (mPartSelectTwoList.contains(rectF) && valid) {
                            twoMatch = true;
                            twoSize++;
                            p2 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectOneList.contains(rectF) && valid) {
                            oneMatch = true;
                            oneSize++;
                            p1 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                    } else if (onePartMatch) {
                        if (mPartSelectOneList.contains(rectF) && valid) {
                            oneMatch = true;
                            oneSize++;
                            p1 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                    }
                }
                if (mListener != null) {
                    getAverageDiff(mIsHistory, oneMatch, twoMatch, threeMatch, fourMatch, fiveMatch, p1, p2, p3, p4, p5, oneSize, twoSize, threeSize, fourSize, fiveSize);
                }
                mPartSelectRetangleIndex = -1;
            } else {
                if (mDrawRealTimeAreaOrPartInfo) {
                    mPartSelectPaint.setColor(Color.BLUE);
                    boolean oneMatch = false, twoMatch = false, threeMatch = false, fourMatch = false, fiveMatch = false;
                    int oneSize = 0, twoSize = 0, threeSize = 0, fourSize = 0, fiveSize = 0;
                    for (int i = 0; i < mLineSize * mRowSize; i++) {
                        RectF rectF = mSmallRectangleList.get(i);
                        int resistance = mResistanceList.get(i);
                        boolean valid = isValidResistance(resistance);
                        if (mPartSelectOneList.contains(rectF) && valid) {
                            oneMatch = true;
                            oneSize++;
                            p1 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectTwoList.contains(rectF) && valid) {
                            twoMatch = true;
                            twoSize++;
                            p2 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectThreeList.contains(rectF) && valid) {
                            threeMatch = true;
                            threeSize++;
                            p3 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectFourList.contains(rectF) && valid) {
                            fourMatch = true;
                            fourSize++;
                            p4 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                        if (mPartSelectFiveList.contains(rectF) && valid) {
                            fiveMatch = true;
                            fiveSize++;
                            p5 += matchPressureByResistance(resistance);
                            canvas.drawRect(rectF, mPartSelectPaint);
                        }
                    }
                    if (mListener != null) {
                        getAverageDiff(mIsHistory, oneMatch, twoMatch, threeMatch, fourMatch, fiveMatch, p1, p2, p3, p4, p5, oneSize, twoSize, threeSize, fourSize, fiveSize);
                    }
                }
            }
        } else {
            if (mListener != null) {
                getAverageDiff(mIsHistory, false, false, false, false, false, p1, p2, p3, p4, p5, 0, 0, 0, 0, 0);
            }
        }
        if (mSelectedRetangleIndex != -1) {
            if ((mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTED || mCurrentPartSelectStatus == PART_SELECT_STATUS_UNSELECTED)
                    && mCurrentAreaSelectStatus != AREA_SELECT_STATUS_SELECTING && mListener != null) {
                mListener.onClick(mResistanceList.get(mSelectedRetangleIndex));
            }
            mSmallRectanglePaint.setColor(Color.RED);
            canvas.drawRect(mSmallRectangleList.get(mSelectedRetangleIndex), mSmallRectanglePaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mScaleGestureDetector.onTouchEvent(event);
        mGestureDetector.onTouchEvent(event);
        return true;
    }

    public void setMaxAndMin(int max, int min, boolean needInvalidate) {
        mMaxResistance = max;
        mMinResistance = min;
        if (needInvalidate) {
            invalidate();
        }
    }

    public void drawRealTimeAreaOrPartSelectInfo(boolean draw) {
        mDrawRealTimeAreaOrPartInfo = draw;
    }

    public void setColorBeanList(List<ColorBean> colorBeans, boolean needInvalidate) {
        mColorBeanList = colorBeans;
        if (needInvalidate) {
            invalidate();
        }
    }

    public boolean isPartSelectFinished() {
        return !mPartSelectOneList.isEmpty() && !mPartSelectTwoList.isEmpty() && !mPartSelectThreeList.isEmpty() && !mPartSelectFourList.isEmpty() && !mPartSelectFiveList.isEmpty();
    }

    public void setAreaSelectStatus(int status) {
        mCurrentAreaSelectStatus = status;
        mSelectedRetangleIndex = -1;
        mPartSelectRetangleIndex = -1;
        mCurrentPartSelectStatus = PART_SELECT_STATUS_UNSELECTED;
        if (mCurrentAreaSelectStatus == AREA_SELECT_STATUS_SELECTING) {
            mAreaSelectPositionList.clear();
        } else if (mCurrentAreaSelectStatus == AREA_SELECT_STATUS_SELECTED) {
            if (mAreaSelectPositionList.size() > 1) {
                mAreaSelectPositionList.add(mAreaSelectPositionList.get(0));
            }
        } else if (mCurrentAreaSelectStatus == AREA_SELECT_STATUS_UNSELECTED) {
            mAreaSelectPositionList.clear();
        }
        invalidate();
    }

    public void setPartSelectSize(int line, int row) {
        mPartSelectLine = line;
        mPartSelectRow = row;
    }

    public void movePartSelectIndex(int direction) {
        if (mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTING) {
            if (direction == PART_SELECT_DIRECTION_TOP) {
                if (mPartSelectRetangleIndex - mRowSize >= 0) {
                    mPartSelectRetangleIndex -= mRowSize;
                }
            } else if (direction == PART_SELECT_DIRECTION_BOTTOM) {
                if (mPartSelectRetangleIndex + mRowSize <= mRowSize * mLineSize) {
                    mPartSelectRetangleIndex += mRowSize;
                }
            } else if (direction == PART_SELECT_DIRECTION_LEFT) {
                if (mPartSelectRetangleIndex % mLineSize > 0) {
                    mPartSelectRetangleIndex -= 1;
                }
            } else if (direction == PART_SELECT_DIRECTION_RIGHT) {
                if ((mPartSelectRetangleIndex + 1) % mLineSize > 0) {
                    mPartSelectRetangleIndex += 1;
                }
            }
            invalidate();
        }
    }

    public int getLineSize() {
        return mLineSize;
    }

    public int getRowSize() {
        return mRowSize;
    }

    public void zoom(boolean bigger) {
        if (bigger) {
            mCurrentScale *= 1.2f;
        } else {
            mCurrentScale *= 0.8f;
        }
        if (mCurrentScale < 1) {
            mCurrentScale = 1;
        }
        mScaleTranslateX = mCurrentScaleRect.centerX() * (1f - mCurrentScale);
        mScaleTranslateY = mCurrentScaleRect.centerY() * (1f - mCurrentScale);
        invalidate();
    }

    public void setPartSelectStatus(int status) {
        mCurrentPartSelectStatus = status;
        if (mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTING || mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTED) {
            mCurrentAreaSelectStatus = AREA_SELECT_STATUS_UNSELECTED;
            mAreaSelectPositionList.clear();
            mSelectedRetangleIndex = -1;
        } else {
            mPartSelectOneList.setEmpty();
            mPartSelectTwoList.setEmpty();
            mPartSelectThreeList.setEmpty();
            mPartSelectFourList.setEmpty();
            mPartSelectFiveList.setEmpty();
        }
        invalidate();
    }

    public void setData(Integer[] data, String deviceId, boolean isHistory) {
        mIsHistory = isHistory;
        for (int i = 0; i < data.length; i++) {
            Integer resistance = data[i];
            if (resistance == null) {
                mResistanceList.set(i, 0);
            } else {
                mResistanceList.set(i, resistance);
            }
        }
        invalidate();
    }

    public void clearData(){
        for (int i = 0; i < mResistanceList.size(); i++) {
            mResistanceList.set(i,0);
        }
        invalidate();
    }

    public void setListener(InnerListener listener) {
        mListener = listener;
    }

    private String formatDouble(double data) {
        return String.format(Locale.getDefault(), "%.2f", data);
    }

    private boolean isValidResistance(int resistance) {
        return mMaxResistance != mMinResistance && mMinResistance < mMaxResistance
                && resistance >= mMinResistance && resistance <= mMaxResistance && resistance > 1;
    }

    private int matchColorByPressure(double pressure) {
        ColorBean first = mColorBeanList.get(0);
        ColorBean last = mColorBeanList.get(mColorBeanList.size() - 1);
        if (pressure > first.getMaxPressure()) {
            String disstrname = first.getColorUpdate();
            if (!TextUtils.isEmpty(disstrname)) {
                return Color.parseColor(disstrname);
            } else {
                return Color.parseColor(first.getColorOrigin());
            }
        }
        if (pressure <= last.getMinPressure()) {
            String disstrname = last.getColorUpdate();
            if (!TextUtils.isEmpty(disstrname)) {
                return Color.parseColor(disstrname);
            } else {
                return Color.parseColor(last.getColorOrigin());
            }
        }
        for (int i = 0; i < mColorBeanList.size(); i++) {
            ColorBean colorBean = mColorBeanList.get(i);
            if (pressure <= colorBean.getMaxPressure() && pressure > colorBean.getMinPressure()) {
                String disstrname = colorBean.getColorUpdate();
                if (!TextUtils.isEmpty(disstrname)) {
                    return Color.parseColor(disstrname);
                } else {
                    String strcolor = colorBean.getColorOrigin();
                    int color = Color.parseColor(strcolor);
                    return color;
                }
            }
        }
        return Color.parseColor("#c4c4c6");
    }

    private double matchPressureByResistance(int resistance) {
        return resistance;
    }

    private void drawSelectedRectangle(float x, float y) {
        float fx = x - mScaleTranslateX - mTranslateX;
        float fy = y - mScaleTranslateY - mTranslateY;
        for (int i = 0; i < mSmallRectangleList.size(); i++) {
            RectF item = mSmallRectangleList.get(i);
            if (item.contains(fx, fy)) {
                if (mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTING) {
                    mPartSelectRetangleIndex = i;
                }
                if (mCurrentPartSelectStatus == PART_SELECT_STATUS_SELECTED || mCurrentPartSelectStatus == PART_SELECT_STATUS_UNSELECTED) {
                    mSelectedRetangleIndex = i;
                }
                if (mCurrentAreaSelectStatus == AREA_SELECT_STATUS_SELECTING) {
                    mSelectedRetangleIndex = i;
                    mAreaSelectPositionList.add(i);
                }
                break;
            }
        }
        invalidate();
    }

    private boolean isCurrentRectangleInAreaSelect(List<Integer> areaSelectPointFList, PointF currentPointF) {
        int nCross = 0;
        for (int i = 0; i < areaSelectPointFList.size(); i++) {
            RectF p1 = mSmallRectangleList.get(areaSelectPointFList.get(i));
            RectF p2 = mSmallRectangleList.get(areaSelectPointFList.get((i + 1) % areaSelectPointFList.size()));
            if (p1.centerX() == p2.centerX())
                continue;
            if (currentPointF.x < Math.min(p1.centerX(), p2.centerX()))
                continue;
            if (currentPointF.x >= Math.max(p1.centerX(), p2.centerX()))
                continue;
            double x = (currentPointF.x - p1.centerX()) * (p2.centerY() - p1.centerY()) / (p2.centerX() - p1.centerX()) + p1.centerY();
            if (x > currentPointF.y) {
                nCross++;
            }
        }
        return (nCross % 2 == 1);
    }

    private void getAverageDiff(boolean isHistory, boolean oneMatch, boolean twoMatch, boolean threeMatch, boolean fourMatch, boolean fiveMatch,
                                double onePressure, double twoPressure, double threePressure, double fourPressure, double fivePressure,
                                int oneSize, int twoSize, int threeSize, int fourSize, int fiveSize) {
        double fiveAvg = 0d, fourAvg = 0d, threeAvg = 0d, twoAvg = 0d, oneAvg = 0d;
        if (oneMatch) {
            oneAvg = onePressure / oneSize;
        }
        if (twoMatch) {
            twoAvg = twoPressure / twoSize;
        }
        if (threeMatch) {
            threeAvg = threePressure / threeSize;
        }
        if (fourMatch) {
            fourAvg = fourPressure / fourSize;
        }
        if (fiveMatch) {
            fiveAvg = fivePressure / fiveSize;
        }
        if (!oneMatch && !twoMatch && !threeMatch && !fourMatch && !fiveMatch) {
            mListener.onPartSelect("0.0", "0.0", "0.0", "0.0", "0.0", "0.0");
        }
        if (oneMatch && !twoMatch && !threeMatch && !fourMatch && !fiveMatch) {
            mListener.onPartSelect(formatDouble(oneAvg), "0.0", "0.0", "0.0", "0.0", "0.0");
        } else if (!oneMatch && twoMatch && !threeMatch && !fourMatch && !fiveMatch) {
            mListener.onPartSelect("0.0", formatDouble(twoAvg), "0.0", "0.0", "0.0", "0.0");
        } else if (!oneMatch && !twoMatch && threeMatch && !fourMatch && !fiveMatch) {
            mListener.onPartSelect("0.0", "0.0", formatDouble(threeAvg), "0.0", "0.0", "0.0");
        } else if (!oneMatch && !twoMatch && !threeMatch && fourMatch && !fiveMatch) {
            mListener.onPartSelect("0.0", "0.0", "0.0", formatDouble(fourAvg), "0.0", "0.0");
        } else if (!oneMatch && !twoMatch && !threeMatch && !fourMatch && fiveMatch) {
            mListener.onPartSelect("0.0", "0.0", "0.0", "0.0", formatDouble(fiveAvg), "0.0");
        }
        if (oneMatch && twoMatch && !threeMatch && !fourMatch && !fiveMatch) {
            double oneToTwoAvg = (onePressure + twoPressure) / (oneSize + twoSize);
            double onePer = oneAvg - oneToTwoAvg;
            double twoPer = twoAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", "0.0(0.0)", "0.0(0.0)", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), "0.0", "0.0", "0.0", formatDouble(oneToTwoAvg));
            }
        } else if (oneMatch && !twoMatch && threeMatch && !fourMatch && !fiveMatch) {
            double oneToTwoAvg = (onePressure + threePressure) / (oneSize + threeSize);
            double onePer = oneAvg - oneToTwoAvg;
            double twoPer = threeAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", formatDouble(twoPer), "0.0", "0.0", formatDouble(oneToTwoAvg));
            }
        } else if (oneMatch && !twoMatch && !threeMatch && fourMatch && !fiveMatch) {
            double oneToTwoAvg = (onePressure + fourPressure) / (oneSize + fourSize);
            double onePer = oneAvg - oneToTwoAvg;
            double twoPer = fourAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", "0.0", formatDouble(twoPer), "0.0", formatDouble(oneToTwoAvg));
            }
        } else if (oneMatch && !twoMatch && !threeMatch && !fourMatch && fiveMatch) {
            double oneToTwoAvg = (onePressure + fivePressure) / (oneSize + fiveSize);
            double onePer = oneAvg - oneToTwoAvg;
            double twoPer = fiveAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", "0.0(0.0)", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", "0.0", "0.0", formatDouble(twoPer), formatDouble(oneToTwoAvg));
            }
        } else if (!oneMatch && twoMatch && threeMatch && !fourMatch && !fiveMatch) {
            double oneToTwoAvg = (twoPressure + threePressure) / (twoSize + threeSize);
            double onePer = twoAvg - oneToTwoAvg;
            double twoPer = threeAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), formatDouble(twoPer), "0.0", "0.0", formatDouble(oneToTwoAvg));
            }
        } else if (!oneMatch && twoMatch && !threeMatch && fourMatch && !fiveMatch) {
            double oneToTwoAvg = (twoPressure + fourPressure) / (twoSize + fourSize);
            double onePer = twoAvg - oneToTwoAvg;
            double twoPer = fourAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), "0.0", formatDouble(twoPer), "0.0", formatDouble(oneToTwoAvg));
            }
        } else if (!oneMatch && twoMatch && !threeMatch && !fourMatch && fiveMatch) {
            double oneToTwoAvg = (twoPressure + fivePressure) / (twoSize + fiveSize);
            double onePer = twoAvg - oneToTwoAvg;
            double twoPer = fiveAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), "0.0", "0.0", formatDouble(twoPer), formatDouble(oneToTwoAvg));
            }
        } else if (!oneMatch && !twoMatch && threeMatch && fourMatch && !fiveMatch) {
            double oneToTwoAvg = (threePressure + fourPressure) / (threeSize + fourSize);
            double onePer = threeAvg - oneToTwoAvg;
            double twoPer = fourAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", "0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect("0.0", "0.0", formatDouble(onePer), formatDouble(twoPer), "0.0", formatDouble(oneToTwoAvg));
            }
        } else if (!oneMatch && !twoMatch && threeMatch && !fourMatch && fiveMatch) {
            double oneToTwoAvg = (threePressure + fivePressure) / (threeSize + fiveSize);
            double onePer = threeAvg - oneToTwoAvg;
            double twoPer = fiveAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", "0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect("0.0", "0.0", formatDouble(onePer), "0.0", formatDouble(twoPer), formatDouble(oneToTwoAvg));
            }
        } else if (!oneMatch && !twoMatch && !threeMatch && fourMatch && fiveMatch) {
            double oneToTwoAvg = (fourPressure + fivePressure) / (fourSize + fiveSize);
            double onePer = fourAvg - oneToTwoAvg;
            double twoPer = fiveAvg - oneToTwoAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", "0.0(0.0)", "0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(oneToTwoAvg));
            } else {
                mListener.onPartSelect("0.0", "0.0", "0.0", formatDouble(onePer), formatDouble(twoPer), formatDouble(oneToTwoAvg));
            }
        }
        if (oneMatch && twoMatch && threeMatch && !fourMatch && !fiveMatch) {
            double oneToThreeAvg = (onePressure + twoPressure + threePressure) / (oneSize + twoSize + threeSize);
            double onePer = oneAvg - oneToThreeAvg;
            double twoPer = twoAvg - oneToThreeAvg;
            double threePer = threeAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), "0.0", "0.0", formatDouble(oneToThreeAvg));
            }
        } else if (oneMatch && twoMatch && !threeMatch && fourMatch && !fiveMatch) {
            double oneToThreeAvg = (onePressure + twoPressure + fourPressure) / (oneSize + twoSize + fourSize);
            double onePer = oneAvg - oneToThreeAvg;
            double twoPer = twoAvg - oneToThreeAvg;
            double threePer = fourAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", "0.0(0.0)", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), "0.0", formatDouble(threePer), "0.0", formatDouble(oneToThreeAvg));
            }
        } else if (oneMatch && twoMatch && !threeMatch && !fourMatch && fiveMatch) {
            double oneToThreeAvg = (onePressure + twoPressure + fivePressure) / (oneSize + twoSize + fiveSize);
            double onePer = oneAvg - oneToThreeAvg;
            double twoPer = twoAvg - oneToThreeAvg;
            double threePer = fiveAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), "0.0", "0.0", formatDouble(threePer), formatDouble(oneToThreeAvg));
            }
        } else if (oneMatch && !twoMatch && threeMatch && fourMatch && !fiveMatch) {
            double oneToThreeAvg = (onePressure + threePressure + fourPressure) / (oneSize + threeSize + fourSize);
            double onePer = oneAvg - oneToThreeAvg;
            double twoPer = threeAvg - oneToThreeAvg;
            double threePer = fourAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", "0.0(0.0)", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", formatDouble(twoPer), formatDouble(threePer), "0.0", formatDouble(oneToThreeAvg));
            }
        } else if (oneMatch && !twoMatch && threeMatch && !fourMatch && fiveMatch) {
            double oneToThreeAvg = (onePressure + threePressure + fivePressure) / (oneSize + threeSize + fiveSize);
            double onePer = oneAvg - oneToThreeAvg;
            double twoPer = threeAvg - oneToThreeAvg;
            double threePer = fiveAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", formatDouble(twoPer), "0.0", formatDouble(threePer), formatDouble(oneToThreeAvg));
            }
        } else if (oneMatch && !twoMatch && !threeMatch && fourMatch && fiveMatch) {
            double oneToThreeAvg = (onePressure + fourPressure + fivePressure) / (oneSize + fourSize + fiveSize);
            double onePer = oneAvg - oneToThreeAvg;
            double twoPer = fourAvg - oneToThreeAvg;
            double threePer = fiveAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", "0.0", formatDouble(twoPer), formatDouble(threePer), formatDouble(oneToThreeAvg));
            }
        } else if (!oneMatch && twoMatch && threeMatch && fourMatch && !fiveMatch) {
            double oneToThreeAvg = (twoPressure + threePressure + fourPressure) / (twoSize + threeSize + fourSize);
            double onePer = twoAvg - oneToThreeAvg;
            double twoPer = threeAvg - oneToThreeAvg;
            double threePer = fourAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", "0.0(0.0)", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), "0.0", formatDouble(oneToThreeAvg));
            }
        } else if (!oneMatch && twoMatch && threeMatch && !fourMatch && fiveMatch) {
            double oneToThreeAvg = (twoPressure + threePressure + fivePressure) / (twoSize + threeSize + fiveSize);
            double onePer = twoAvg - oneToThreeAvg;
            double twoPer = threeAvg - oneToThreeAvg;
            double threePer = fiveAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), formatDouble(twoPer), "0.0", formatDouble(threePer), formatDouble(oneToThreeAvg));
            }
        } else if (!oneMatch && twoMatch && !threeMatch && fourMatch && fiveMatch) {
            double oneToThreeAvg = (twoPressure + fourPressure + fivePressure) / (twoSize + fourSize + fiveSize);
            double onePer = twoAvg - oneToThreeAvg;
            double twoPer = fourAvg - oneToThreeAvg;
            double threePer = fiveAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), "0.0", formatDouble(twoPer), formatDouble(threePer), formatDouble(oneToThreeAvg));
            }
        } else if (!oneMatch && !twoMatch && threeMatch && fourMatch && fiveMatch) {
            double oneToThreeAvg = (threePressure + fourPressure + fivePressure) / (threeSize + fourSize + fiveSize);
            double onePer = threeAvg - oneToThreeAvg;
            double twoPer = fourAvg - oneToThreeAvg;
            double threePer = fiveAvg - oneToThreeAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", "0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(oneToThreeAvg));
            } else {
                mListener.onPartSelect("0.0", "0.0", formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), formatDouble(oneToThreeAvg));
            }
        }
        if (oneMatch && twoMatch && threeMatch && fourMatch && !fiveMatch) {
            double oneToFourAvg = (onePressure + twoPressure + threePressure + fourPressure) / (oneSize + twoSize + threeSize + fourSize);
            double onePer = oneAvg - oneToFourAvg;
            double twoPer = twoAvg - oneToFourAvg;
            double threePer = threeAvg - oneToFourAvg;
            double fourPer = fourAvg - oneToFourAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(fourAvg) + "(" + formatDouble(fourPer) + ")", "0.0(0.0)", formatDouble(oneToFourAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), formatDouble(fourPer), "0.0", formatDouble(oneToFourAvg));
            }
        } else if (oneMatch && twoMatch && threeMatch && !fourMatch && fiveMatch) {
            double oneToFourAvg = (onePressure + twoPressure + threePressure + fivePressure) / (oneSize + twoSize + threeSize + fiveSize);
            double onePer = oneAvg - oneToFourAvg;
            double twoPer = twoAvg - oneToFourAvg;
            double threePer = threeAvg - oneToFourAvg;
            double fourPer = fiveAvg - oneToFourAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", "0.0(0.0)", formatDouble(fourAvg) + "(" + formatDouble(fourPer) + ")", formatDouble(oneToFourAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), "0.0", formatDouble(fourPer), formatDouble(oneToFourAvg));
            }
        } else if (oneMatch && twoMatch && !threeMatch && fourMatch && fiveMatch) {
            double oneToFourAvg = (onePressure + twoPressure + fourPressure + fivePressure) / (oneSize + twoSize + fourSize + fiveSize);
            double onePer = oneAvg - oneToFourAvg;
            double twoPer = twoAvg - oneToFourAvg;
            double threePer = fourAvg - oneToFourAvg;
            double fourPer = fiveAvg - oneToFourAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", "0.0(0.0)", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(fourAvg) + "(" + formatDouble(fourPer) + ")", formatDouble(oneToFourAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), "0.0", formatDouble(threePer), formatDouble(fourPer), formatDouble(oneToFourAvg));
            }
        } else if (oneMatch && !twoMatch && threeMatch && fourMatch && fiveMatch) {
            double oneToFourAvg = (onePressure + threePressure + fourPressure + fivePressure) / (oneSize + threeSize + fourSize + fiveSize);
            double onePer = oneAvg - oneToFourAvg;
            double twoPer = threeAvg - oneToFourAvg;
            double threePer = fourAvg - oneToFourAvg;
            double fourPer = fiveAvg - oneToFourAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", "0.0(0.0)", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(fourAvg) + "(" + formatDouble(fourPer) + ")", formatDouble(oneToFourAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), "0.0", formatDouble(twoPer), formatDouble(threePer), formatDouble(fourPer), formatDouble(oneToFourAvg));
            }
        } else if (!oneMatch && twoMatch && threeMatch && fourMatch && fiveMatch) {
            double oneToFourAvg = (twoPressure + threePressure + fourPressure + fivePressure) / (twoSize + threeSize + fourSize + fiveSize);
            double onePer = twoAvg - oneToFourAvg;
            double twoPer = threeAvg - oneToFourAvg;
            double threePer = fourAvg - oneToFourAvg;
            double fourPer = fiveAvg - oneToFourAvg;
            if (isHistory) {
                mListener.onPartSelect("0.0(0.0)", formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(fourAvg) + "(" + formatDouble(fourPer) + ")", formatDouble(oneToFourAvg));
            } else {
                mListener.onPartSelect("0.0", formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), formatDouble(fourPer), formatDouble(oneToFourAvg));
            }
        }
        if (oneMatch && twoMatch && threeMatch && fourMatch && fiveMatch) {
            double oneToFiveAvg = (onePressure + twoPressure + threePressure + fourPressure + fivePressure) / (oneSize + twoSize + threeSize + fourSize + fiveSize);
            double onePer = oneAvg - oneToFiveAvg;
            double twoPer = twoAvg - oneToFiveAvg;
            double threePer = threeAvg - oneToFiveAvg;
            double fourPer = fourAvg - oneToFiveAvg;
            double fivePer = fiveAvg - oneToFiveAvg;
            if (isHistory) {
                mListener.onPartSelect(formatDouble(oneAvg) + "(" + formatDouble(onePer) + ")", formatDouble(twoAvg) + "(" + formatDouble(twoPer) + ")", formatDouble(threeAvg) + "(" + formatDouble(threePer) + ")", formatDouble(fourAvg) + "(" + formatDouble(fourPer) + ")", formatDouble(fiveAvg) + "(" + formatDouble(fivePer) + ")", formatDouble(oneToFiveAvg));
            } else {
                mListener.onPartSelect(formatDouble(onePer), formatDouble(twoPer), formatDouble(threePer), formatDouble(fourPer), formatDouble(fivePer), formatDouble(oneToFiveAvg));
            }
        }
    }

    private class SGDListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(@NonNull ScaleGestureDetector detector) {
            mCurrentScale *= detector.getScaleFactor();
            if (mCurrentScale < 1) {
                mCurrentScale = 1;
                mTranslateX = 0;
                mTranslateY = 0;
            }
            float m2FingerCenterX = detector.getFocusX();
            float m2FingerCenterY = detector.getFocusY();
            mScaleTranslateX = m2FingerCenterX * (1 - mCurrentScale);
            mScaleTranslateY = m2FingerCenterY * (1 - mCurrentScale);
            invalidate();
            return true;
        }
    }

    private class GDListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(@NonNull MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
            drawSelectedRectangle(x, y);
            return true;
        }

        @Override
        public boolean onScroll(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
            mTranslateX -= distanceX;
            mTranslateY -= distanceY;
            invalidate();
            return true;
        }
    }

    public interface InnerListener {
        void onClick(int resistance);

        void onAreaSelect(String pressure);

        void onPartSelect(String one, String two, String three, String four, String five, String oneToFive);

        void onTotalPressure(boolean isHistory, String pressure);
    }
}
